package com.itheima.web.servlset;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Xiao
 * @date 2021/9/4
 */
@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servletDemo01 doGet...");
        //tomcate在启动项目后，创建一个ServletContext对象，这个对象就可以代表这个项目
        //1. 如何获取
        ServletContext servletContext1 = getServletContext();
        ServletContext servletContext2 = req.getServletContext();
        System.out.println(servletContext1 == servletContext2); //true
        //2. 有哪些作用  动态获取资源路径
        String path = servletContext1.getRealPath("hello.jsp");
        System.out.println(path);
        //D:\develop\IdeaProjects\JavaWeb\mavenfiles\Filter_Listener\src\main\webapp\hello.jsp
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
